//一些通用js

var getImgUrl = function (source) {

    if (source == "") return "";

    if (startWith("/u", source)) {
        return "http://img4.nahuo.com" + source;
    } else if (startWith("http", source)) {
        return source;
    }

    var words = source.split(":");

    return "https://" + words[1] + ".b0.upaiyun.com" + words[2];
}

var startWith = function (str, source) {
    var reg = new RegExp("^" + str);
    return reg.test(source);
}



var getUrl = function (fileId, size) {
    /// <summary>
    /// 获取图片地址
    /// </summary>
    /// <param name="fileId">图片存储信息</param>
    /// <param name="size">图片大小（最长边），空则返回原图地址</param>

    var serviceName = fileId.indexOf("upyun:") == 0 ? "upyun" : "nahuo";
    switch (serviceName) {
        case "upyun":
            return (function (fileId, size) {

                if (!fileId) return "";
                if (fileId.indexOf("upyun:") != 0) return "";

                var arry = fileId.split(":");
                if (arry.length != 3)
                    return fileId;

                if ('nahuo-img-server' == arry[1])
                    return "https://" + arry[1] + ".b0.upaiyun.com" + arry[2] + "!thum." + size;

                var imgUrl = "https://" + arry[1] + ".b0.upaiyun.com" + arry[2];
                if (size && size > 0) {

                    var s = "";
                    if (size <= 60)
                        s = "a1";
                    else if (60 < size && size <= 80)
                        s = "a2";
                    else if (80 < size && size <= 100)
                        s = "a3";
                    else if (100 < size && size <= 120)
                        s = "a4";
                    else if (120 < size && size <= 140)
                        s = "a5";
                    else if (140 < size && size <= 160)
                        s = "a6";
                    else if (160 < size && size <= 180)
                        s = "a7";
                    else if (180 < size && size <= 200)
                        s = "a8";
                    else if (200 < size && size <= 240)
                        s = "a9";
                    else if (240 < size && size <= 280)
                        s = "a10";
                    else if (280 < size && size <= 320)
                        s = "a11";
                    else if (320 < size && size <= 360)
                        s = "a12";
                    else if (360 < size && size <= 400)
                        s = "a13";
                    else if (400 < size && size <= 440)
                        s = "a14";
                    else if (440 < size && size <= 480)
                        s = "a15";
                    else if (480 < size && size <= 520)
                        s = "a16";
                    else if (520 < size && size <= 580)
                        s = "a17";
                    else if (580 < size && size <= 600)
                        s = "a18";
                    else if (600 < size && size <= 700)
                        s = "a19";
                    else if (700 < size && size <= 800)
                        s = "a20";
                    else if (800 < size && size <= 900)
                        s = "a21";
                    else if (900 < size && size <= 1000)
                        s = "a22";
                    else if (1000 < size && size <= 1100)
                        s = "a23";
                    else if (1100 < size && size <= 1200)
                        s = "a24";

                    return imgUrl + "!" + s;

                } else
                    return imgUrl;
            })(fileId, size);
        case "nahuo":
            return (function (fileId, size) {
                if (!fileId) return "";
                if (fileId.indexOf("http://") == 0) return fileId;
                else if (fileId.indexOf("/u") == 0) {
                    if (size && size > 0)
                        return "http://img4.nahuo.com" + fileId + "!thum." + size;
                    else
                        return "http://img4.nahuo.com" + fileId;
                }
                else {
                    if (size && size > 0)
                        return "https://img3.nahuo.com/" + fileId + "-" + size + "-" + size + ".jpg";
                    else
                        return "https://img3.nahuo.com/" + fileId;
                }
            })(fileId, size);
        default: return fileId;
    }
}
/*getUrl:function(){
    return "aaa";
}*/





//模块输出
module.exports = {
    getImgUrl: getImgUrl,
    getUrl: getUrl
}
